﻿#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using Drill.Core;
using NUnit.Framework;

namespace Drill.Configuration
{
	[TestFixture]
	public class ConfigurationHelperFactoryTests
	{
		[Test]
		public void CanConstruct()
		{
			// Arrange
			// Nothing to arrange.

			// Act
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Assert
			Assert.That(factory, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithSinglePathProviderAndExplicitNullSearchPaths()
		{
			// Arrange
			IRootPathProvider pathProvider = new ExecutingDirectoryRootPathProvider();
			List<ISearchPath> searchPaths = null;
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			IConfigurationHelper configurationHelper = factory.CreateConfigurationHelper(pathProvider, searchPaths);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			Assert.That(configurationHelper, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithSinglePathProviderAndOmittedSearchPaths()
		{
			// Arrange
			IRootPathProvider pathProvider = new ExecutingDirectoryRootPathProvider();
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			IConfigurationHelper configurationHelper = factory.CreateConfigurationHelper(pathProvider);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			Assert.That(configurationHelper, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithSinglePathProviderAndSearchPaths()
		{
			// Arrange
			IRootPathProvider pathProvider = new ExecutingDirectoryRootPathProvider();
			List<ISearchPath> searchPaths = new List<ISearchPath> { new SearchPath { Value = @"SomeFakeDirectory\Config" } };
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			IConfigurationHelper configurationHelper = factory.CreateConfigurationHelper(pathProvider, searchPaths);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			Assert.That(configurationHelper, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "rootPathProvider", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotCreateInstanceWithNullPathProvider()
		{
			// Arrange
			IRootPathProvider pathProvider = null;
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			factory.CreateConfigurationHelper(pathProvider);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanCreateInstanceWithListOfPathProviderAndExplicitNullSearchPaths()
		{
			// Arrange
			List<IRootPathProvider> pathProviders = 
				new List<IRootPathProvider>
					{
						new ExecutingDirectoryRootPathProvider()
					};
			List<ISearchPath> searchPaths = null;
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			IConfigurationHelper configurationHelper = factory.CreateConfigurationHelper(pathProviders, searchPaths);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			Assert.That(configurationHelper, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithListOfPathProviderAndOmittedSearchPaths()
		{
			// Arrange
			List<IRootPathProvider> pathProviders = 
				new List<IRootPathProvider>
					{
						new ExecutingDirectoryRootPathProvider()
					};
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			IConfigurationHelper configurationHelper = factory.CreateConfigurationHelper(pathProviders);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			Assert.That(configurationHelper, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithListOfPathProvideSearchPaths()
		{
			// Arrange
			List<IRootPathProvider> pathProviders = 
				new List<IRootPathProvider>
					{
						new ExecutingDirectoryRootPathProvider()
					};
			List<ISearchPath> searchPaths = new List<ISearchPath> { new SearchPath { Value = @"SomeFakeDirectory\Config" } };
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			IConfigurationHelper configurationHelper = factory.CreateConfigurationHelper(pathProviders, searchPaths);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			Assert.That(configurationHelper, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "rootPathProviders", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotCreateInstanceWithNullListOfPathProvider()
		{
			// Arrange
			List<IRootPathProvider> pathProviders = null;
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			factory.CreateConfigurationHelper(pathProviders);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ArgumentException), ExpectedMessage = "rootPathProviders list is empty", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotCreateInstanceWithEmptyListOfPathProvider()
		{
			// Arrange
			List<IRootPathProvider> pathProviders = new List<IRootPathProvider>();
			IConfigurationHelperFactory factory = new ConfigurationHelperFactory();

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			factory.CreateConfigurationHelper(pathProviders);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}
	}
}